package com.example.administrator.activity.Homework.Homework4.Homework4_1;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.administrator.activity.parcitise.Activity.AboutActivity;
import com.example.administrator.activity.R;

import java.text.DecimalFormat;

public class BMI extends AppCompatActivity {
    /**
     * OnClickLIstener  setOnClickListener() 点击事件
     * findViewById() 关联
     * Double.parseDouble（） 强转Double
     * DecimalFormat 保留数位
     * Intent 转换
     * AlertDialog.Builder 对话框
     */
    private Button ch2_bmi_btn_computer, ch2_bmi_btn_about, ch2_bmi_btn_exit;
    private EditText ch2_bmi_ed_height, ch2_bmi_ed_weight;
    private TextView ch2_bmi_tv_result;

    /**
     * 1.关联布局文件
     * 2.关联控件
     * 3.按钮 点击事件
     * 按钮要加监听
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bmi);
        /**
         * 初始化方法：关联控件
         */

        init();
        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 * View v这个参数表示点击的是哪个控件
                 */

                switch (v.getId()) {
                    case R.id.ch2_bmi_btn_computer:
                        computer();
                        break;
                    case R.id.ch2_bmi_btn_about:
                        about();
                        break;
                    case R.id.ch2_bmi_btn_exit:
                        exit();
                        break;
                }
            }
        };
        ch2_bmi_btn_computer.setOnClickListener(clickListener);
        ch2_bmi_btn_about.setOnClickListener(clickListener);
        ch2_bmi_btn_exit.setOnClickListener(clickListener);

    }

    /**
     * 退出按钮的操作
     * // 创建对话框步骤：
     * // 1、创建一个AlertDialog对象
     * AlertDialog.Builder dialog=new AlertDialog.Builder(Ch2BMIActivity.this);
     * // 2、设置对话框标题
     * // 3、设置对话框显示的内容
     * // 4、添加对话框的“确定”、“取消”按钮
     * // 5、显示对话框
     */
    private void exit() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("退出？");
        builder.setMessage("真的要退出系统吗？");
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
//                BMI.this.finish();
                System.exit(0);
            }
        });

        builder.show();
    }

    /**
     * 关于按钮的操作
     * 跳转到另一个页面
     */
    private void about() {
        Intent intent = new Intent(BMI.this, AboutActivity.class);
        /**
         * 传递数据
         */
        intent.putExtra("test", "aaa");
        startActivity(intent);


    }

    /**
     * 计算按钮
     */
    private void computer() {
        /**
         * 先获取输入框的内容，再根据公式计算BMI，根据BMI的值，过轻还是正常,最后显示
         */
        String s_height = ch2_bmi_ed_height.getText().toString();
        double height = Double.parseDouble(s_height);
        String s_weight = ch2_bmi_ed_weight.getText().toString();
        double weight = Double.parseDouble(s_weight);
        double bmi = weight / (height / 100) / (height / 100);
                        /*
                        结果保留两位小数
                         */
        DecimalFormat format = new DecimalFormat("0.00");
        String s = "您的体重指数为：" + format.format(bmi) + "\n理想指数为20-22\n";
        if (bmi < 18.5) {
            s += "过轻";
        } else if (bmi < 23) {
            s += "正常";
        } else if (bmi < 25) {
            s += "过重";
        } else if (bmi < 30) {
            s += "肥胖";
        } else {
            s += "痴肥";
        }
        ch2_bmi_tv_result.setText(s);

    }

    private void init() {
        ch2_bmi_btn_about = findViewById(R.id.ch2_bmi_btn_about);
        ch2_bmi_btn_exit = findViewById(R.id.ch2_bmi_btn_exit);
        ch2_bmi_btn_computer = findViewById(R.id.ch2_bmi_btn_computer);
        ch2_bmi_ed_height = findViewById(R.id.ch2_bmi_ed_height);
        ch2_bmi_ed_weight = findViewById(R.id.ch2_bmi_ed_weight);
        ch2_bmi_tv_result = findViewById(R.id.ch2_bmi_tv_result);

    }
}
